package com.emenu;

import java.util.ArrayList;
import java.util.List;

import com.emenu.FunhitFragment.Listview1Adapter;
import com.emenu.FunhitFragment.Listview2Adapter;
import com.emenu.FunhitFragment.getMasDetailTask;
import com.emenu.FunhitFragment.getMasTask;
import com.emenu.application.StaticObject;
import com.emenu.bean.BusHitDetail;
import com.emenu.bean.BusHitMaster;
import com.emenu.bean.PubPrinterTable;
import com.emenu.bean.PubTable;
import com.emenu.bean.SubMenu;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class FunMenuHitFragment extends Fragment {
	ListView listView1;
	ListView listView2;
	public EditText fun_mhtab;
	public Button clear;
	public TextView mhhittype;
	public Button mhchgtype;
	public Button mhsend;

	public StaticObject SO;
	Context cn;

	List<SubMenu> sublist = new ArrayList<SubMenu>();
	// List<BusHitMaster> list = new ArrayList<BusHitMaster>();
	List<BusHitDetail> havelist = new ArrayList<BusHitDetail>();

	String bhdsubcode;
	private int hit_posttype = 0;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		SO = (StaticObject) activity.getApplication();
		havelist = SO.getBusHitDetailList();

		cn = this.getActivity();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View v = inflater.inflate(R.layout.funmenuhit, container, false);

		findViewsById(v);
		mhhittype.setText("正常");
		setListener();

		return v;
	}

	/** 视图 样式 */
	private void findViewsById(View v) {
		fun_mhtab = (EditText) v.findViewById((R.id.fun_mhtab));
		clear = (Button) v.findViewById((R.id.mhclear));
		mhhittype = (TextView) v.findViewById((R.id.mhhittype));
		mhchgtype = (Button) v.findViewById((R.id.mhchgtype));
		mhsend = (Button) v.findViewById((R.id.mhsend));

		listView1 = (ListView) v.findViewById(R.id.hitlistView1);
		listView2 = (ListView) v.findViewById(R.id.hitlistView2);

	}

	/** 设置监听器 */
	private void setListener() {
		fun_mhtab.addTextChangedListener(txwatcher);
		listView1.setOnItemClickListener(listview1listener);
		listView2.setOnItemClickListener(listview2listener);

		clear.setOnClickListener(clearListener);
		mhchgtype.setOnClickListener(mhchgtypeListener);
		mhsend.setOnClickListener(mhsendListener);
	}

	private TextWatcher txwatcher = new TextWatcher() {
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub

		}

		@Override
		public void afterTextChanged(Editable s) {
			sublist.clear();

			for (SubMenu sm : SO.getSubMenuList()) {
				boolean iscon = sm.getHelpcode().contains(
						fun_mhtab.getText() + "");
				if (iscon) {
					sublist.add(sm);

					ListviewFM1Adapter adapter = new ListviewFM1Adapter();
					listView1.setAdapter(adapter);
				}
			}
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {

		}

	};
//菜品列表
	public class ListviewFM1Adapter extends BaseAdapter {

		@Override
		public int getCount() {

			return sublist.size();
		}

		@Override
		public Object getItem(int position) {

			return sublist.get(position);
		}

		@Override
		public long getItemId(int position) {

			return position;

		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = ((FunMainActivity) cn).getLayoutInflater()
						.inflate(R.layout.funmenuhitsublist, null);
			}

			TextView funsubname;
			TextView funsubtagname;
			TextView funsubhelpcode;
			TextView funsubprice;

			funsubname = (TextView) convertView.findViewById(R.id.funsubname);
			funsubtagname = (TextView) convertView
					.findViewById(R.id.funsubtagname);
			funsubhelpcode = (TextView) convertView
					.findViewById(R.id.funsubhelpcode);
			funsubprice = (TextView) convertView.findViewById(R.id.funsubprice);

			funsubname.setText(sublist.get(position).getName());
			funsubtagname.setText(sublist.get(position).getTag_name());

			funsubhelpcode.setText(sublist.get(position).getHelpcode());
			funsubprice.setText(sublist.get(position).getPrice() + "");

			return convertView;
		}

	}

	private OnItemClickListener listview1listener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// 选中的点菜明细表
			SubMenu sm = sublist.get(arg2);

			// 点菜明细
			BusHitDetail bhd = new BusHitDetail();
			bhd.setBig_Menu_code(sm.getMenu_code());

			bhd.setID(-1);
			bhd.setHit_code("-1");
			bhd.setMenucode(sm.getCode());
			bhd.setMenuname(sm.getName());
			bhd.setPrice(sm.getPrice());
			bhd.setAmount(Double.parseDouble("1"));
			bhd.setTag_code(sm.getTag_code());
			bhd.setTag_name(sm.getTag_name());
			bhd.setAmount_sum(Math.round(sm.getPrice() * 1));
			bhd.setMenu_type("-1");
			bhd.setRljtype(sm.getRljtype());
			bhd.setCalledSingle(0);
			bhd.setRemark("");
			bhd.setUpload(0);
			bhd.setC1("");
			bhd.setC2("");
			bhd.setC3("");

			havelist.add(bhd);
			
			ListviewFM2Adapter adapter2 = new ListviewFM2Adapter();
			listView2.setAdapter(adapter2);

		}

	};
//已点菜品列表
	public class ListviewFM2Adapter extends BaseAdapter {

		@Override
		public int getCount() {

			return havelist.size();
		}

		@Override
		public Object getItem(int position) {

			return havelist.get(position);
		}

		@Override
		public long getItemId(int position) {

			return position;

		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = ((FunMainActivity) cn).getLayoutInflater()
						.inflate(R.layout.funmenuhitbhdlist, null);
			}

			TextView funbhdname;
			TextView funbhdtagname;
			TextView funbhdamount;
			TextView funbhdprice;
			TextView funbhdsumamount;
			funbhdname = (TextView) convertView.findViewById(R.id.funbhdname);
			funbhdtagname = (TextView) convertView
					.findViewById(R.id.funbhdtagname);
			funbhdamount = (TextView) convertView
					.findViewById(R.id.funbhdamount);
			funbhdprice = (TextView) convertView.findViewById(R.id.funbhdprice);
			funbhdsumamount = (TextView) convertView
					.findViewById(R.id.funbhdsumamount);

			funbhdname.setText(havelist.get(position).getMenuname());
			funbhdtagname.setText(havelist.get(position).getTag_name());

			funbhdamount.setText(havelist.get(position).getAmount() + "");
			funbhdprice.setText(havelist.get(position).getPrice() + "");
			funbhdsumamount
					.setText(havelist.get(position).getAmount_sum() + "");

			return convertView;
		}

	}

	private OnItemClickListener listview2listener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// 选中的点菜明细表
			bhdsubcode = havelist.get(arg2).getMenucode();

		}

	};

	private OnClickListener clearListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			fun_mhtab.setText("");
		}

	};

	private OnClickListener mhchgtypeListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			hit_posttype++;
			int j = 0;
			j = hit_posttype % 4;

			hit_posttype = j;

			if (j == 0) {
				mhhittype.setText("正常");
			} else if (j == 1) {
				mhhittype.setText("加菜");
			} else if (j == 2) {
				mhhittype.setText("赠菜");
			} else if (j == 3) {
				mhhittype.setText("退菜");
			}
		}

	};

	private OnClickListener mhsendListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(cn, SurePost.class);

			// 启动Activity
			startActivity(intent);
		}

	};

}
